<template>
  <div class="loginContainer">
    <!-- 头像 -->
    <div class="avatarBox">
      <img src="../assets/logo.png" alt />
    </div>
    <!-- 表单 -->
    <el-form
      :model="loginForm"
      :rules="loginFormRules"
      ref="loginFormRef"
      label-width="0px"
      class="boxBox"
    >
      <el-form-item prop="username">
        <el-input placeholder="请输入账号" v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
      </el-form-item>
      <el-form-item prop="authCode">
        <el-input
          placeholder="请输入密码"
          type="password"
          v-model="loginForm.authCode"
          prefix-icon="iconfont icon-3702mima"
        ></el-input>
      </el-form-item>
      <el-form-item class="btnBox">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="resetLoginForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '15013349143',
        authCode: '123456',
        type: 1
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        authCode: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        console.log(valid)
        if (!valid) {
          return
        }

        const { data: res } = await this.$http.post(
          'open/login',
          this.loginForm
        )
        if (res.code !== 200) {
          console.log(res)
          this.$message.error(res.message)
          return
        }
        this.$message.success(res.message)
        console.log(res)
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.loginContainer {
  height: 300px;
  width: 500px;
  position: absolute;
  background-color: #fff;
  border-radius: 1%;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatarBox {
    width: 130px;
    height: 130px;
    border: 1 solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    // background-color: red;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border: 1px solid #eee;
      border-radius: 50%;
    }
  }
  .boxBox {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    .btnBox {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
